package com.topvision.face.util;

import android.util.Log;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.reflect.Constructor;

public class ExceptionUtil {
	
	/**
	 * 获得异常的详细信息
	 * 
	 * @param e 异常的类
	 * @return
	 */
	public static String getDetail(Exception e) {
		 StringWriter writer = new StringWriter();
		  e.printStackTrace(new PrintWriter(writer,true));
		  return writer.toString();
	}
	
	/**
	 * 根据异常的类型，创建错误异常的
	 * @param <T> 
	 * @param t 异常的类型
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static <T extends Exception> T createException(Class<T> t, String msg){
		try {
			Constructor<T> tc = t.getDeclaredConstructor(String.class);
			return tc.newInstance(msg);
		} catch (Exception e) {
			Log.e("sk", getDetail(e));
		}
		return (T) new Exception(msg);
	}
	
	
	
	
	
}
